package com.lancoo.znbkxx.teacher.aibkTestCoach.aibkPublicActivity

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ErrorRecovery
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.lancoo.znbkxx.teacher.utils.SoftKeyBoardListener
import kotlinx.android.synthetic.main.activity_error_recovery.*
import kotlinx.android.synthetic.main.activity_error_recovery.EdtErrorDescribe
import kotlinx.android.synthetic.main.activity_error_recovery.GifLoading
import kotlinx.android.synthetic.main.activity_error_recovery.RvErrorRecovery
import org.koin.androidx.viewmodel.ext.android.viewModel

class ActivityErrorRecovery : KBaseActivity(), View.OnClickListener {

    private var quesId: String = ""

    companion object {
        fun launch(context: Context, quesId: String) {
            val intent = Intent(context, ActivityErrorRecovery::class.java)
            intent.putExtra("QuesID", quesId)
            context.startActivity(intent)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()
    private var errorRecovery: List<ErrorRecovery>? = null

    override fun setLayout(): Int {
        return R.layout.activity_error_recovery
    }

    override fun initView() {
        setSoftKeyBoardListener()
        BtnSubmitErrorRecovery.setOnClickListener(this)
        BtnBack.setOnClickListener(this)

        getErrorConfig()
    }

    override fun initData() {
        quesId = intent.getStringExtra("QuesID")
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.BtnSubmitErrorRecovery -> {
                var errorType = ""
                val size = errorRecovery!!.size
                for (i in 0 until size) {
                    val priority = errorRecovery!![i].Priority
                    if (priority == 1) {
                        val error = errorRecovery!![i].Error
                        if (errorType.isEmpty()) {
                            errorType = error
                        } else {
                            errorType = "$errorType、$error"
                        }
                    }
                }
                val errorDescribe = EdtErrorDescribe.text.toString()

                if (errorType.isEmpty() && errorDescribe.isEmpty()) {
                    MyToast.showToast("请至少选择一个错误类型或填写错误描述")
                } else {
                    submitErrorRecovery(errorType, errorDescribe)
                }
            }
            R.id.BtnBack -> {
                finish()
            }
        }
    }

    /**
     * 获取纠错类型
     * */
    private fun getErrorConfig() {
        mViewModel.getErrorConfig()
                .doOnSubscribe {
                    GifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    GifLoading.visibility = View.GONE
                    errorRecovery = it.data
                    showErrorRecoveryView(errorRecovery!!)

                }, {
                    MyToast.showToast(it.message)
                    Handler().postDelayed({
                        finish()
                    }, 500)
                })
    }

    /*
    * 加载界面信息
    * */
    private fun showErrorRecoveryView(errorRecovery: List<ErrorRecovery>) {
        val manager = GridLayoutManager(this, 3)
        RvErrorRecovery.layoutManager = manager
        val mAdapter = AibkAdapterErrorRecovery(this, errorRecovery)
        RvErrorRecovery.adapter = mAdapter
    }

    /**
     * 提交纠错结果
     * */
    private fun submitErrorRecovery(errorType: String, errorDescribe: String) {
        mViewModel.addQuesErrorCorrection(quesId, errorType, errorDescribe)
                .doOnSubscribe {
                    startLoad()
                }.bindLifeCycle(this)
                .subscribe({
                    stopLoad()
                    MyToast.showToast("提交成功")
                    Handler().postDelayed({
                        finish()
                    }, 500)
                }, {
                    stopLoad()
                    MyToast.showToast("提交失败")
                })
    }

    /**
     * 添加软键盘监听
     */
    private fun setSoftKeyBoardListener() {
        val softKeyBoardListener = SoftKeyBoardListener(this)
        //软键盘状态监听
        softKeyBoardListener.setListener(object : SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
            override fun keyBoardShow(height: Int) {
                //软键盘已经显示，做逻辑
                ShadowContainer.visibility = View.GONE
            }

            override fun keyBoardHide(height: Int) {
                //软键盘已经隐藏,做逻辑
                Handler().postDelayed({
                    ShadowContainer.visibility = View.VISIBLE
                }, 300)

            }
        })
    }

}